package io.github.cchenxi.crpc.simple.example.consumer;

import java.util.Arrays;

import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.github.cchenxi.crpc.core.annotation.RpcConsumer;
import io.github.cchenxi.crpc.core.consumer.ConsumerConfig;
import io.github.cchenxi.crpc.simple.example.api.User;
import io.github.cchenxi.crpc.simple.example.api.UserService;

@SpringBootApplication
@RestController
@Import({ConsumerConfig.class})
public class CrpcSimpleExampleConsumerApplication {

    @RpcConsumer
    UserService userService;

    @GetMapping("user")
    public User findById(@RequestParam(name = "id") int id) {
        return userService.findById(id);
    }

    public static void main(String[] args) {
        SpringApplication.run(CrpcSimpleExampleConsumerApplication.class, args);
    }

    @Bean
    public ApplicationRunner consumerRun() {
        return x -> {
            User user = userService.findById(100);
            System.out.println("RPC result userService.findById(100) ==> " + user);

            String name = userService.getName(101);
            System.out.println("RPC result userService.getName ==> " + name);

            User user01 = userService.findById(100, "user01");
            System.out.println("RPC result userService.findById(100, \"user01\")" + user01);

            long idl = userService.getId(102L);
            System.out.println("RPC result userService.getId(102L) ==> " + idl);

            long idf = userService.getId(10.0f);
            System.out.println("RPC result userService.getId(10.0f) ==> " + idf);

            long id = userService.getId(new User(1000, "chenxi"));
            System.out.println("RPC result userService.getId(new User(1000, \"chenxi\")) ==> " + id);

            int[] idia = userService.getIds(1);
            System.out.println("RPC result userService.getIds(1) ==> " + Arrays.toString(idia));

            float[] idfa = userService.getIds(1.0f);
            System.out.println("RPC result userService.getIds(1.0f) ==> " + Arrays.toString(idfa));

            long[] idls = userService.getIdsFromIds(new int[]{10, 20, 30});
            System.out.println("RPC result userService.getIdsFromIds(new int[]{10, 20, 30}) ==> " + Arrays.toString(idls));
        };
    }
}
